<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="所属年份" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          value-format="YYYY"
          type="year"
          placeholder="选择所属年份"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="员工" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择员工"
          clearable
          class="!w-240px" multiple collapse-tags
        >
          <el-option
            v-for="dict in userOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="指标点" prop="performancePoint">
        <el-select
          v-model="queryParams.performancePoint"
          placeholder="请选择指标点"
          clearable
          class="!w-240px" multiple collapse-tags
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CRM_PERFORMANCE_POINT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
    >
      <el-table-column label="所属年月" align="center" prop="belongMonth" />
      <el-table-column label="员工" align="center" prop="userId">
        <template #default="scope">
          <arr-dict-tag :dict-arr="userOptions" :value="scope.row.userId" />
        </template>
      </el-table-column>
      <el-table-column label="指标点" align="center" prop="performancePoint">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_PERFORMANCE_POINT" :value="scope.row.performancePoint" />
        </template>
      </el-table-column>
      <el-table-column label="目标值" align="center" prop="targetValue" />
      <el-table-column label="完成值" align="center" prop="finishValue" />
      <el-table-column label="完成率" align="center" prop="finishRate" />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { PersonPerformanceConfigApi, PersonPerformanceConfig } from '@/api/crm/performance/person'
import * as UserApi from "@/api/system/user";

/** CRM_业绩配置 列表 */
defineOptions({ name: 'PersonPerformanceConfig' })
const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PersonPerformanceConfig[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  year: undefined,
  userId: undefined,
  performancePoint: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PersonPerformanceConfigApi.getPerformanceResultPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(async () => {
  // 获得用户列表
  const userDataArr =  await UserApi.getSimpleUserList();
  let arrTemp = []
  for (const element of userDataArr) {
    arrTemp.push({ 'label': element.nickname, 'value': element.id+'' } )
  }
  userOptions.value = arrTemp
  // console.debug('userOptions', userOptions)
  getList()
})
</script>
